﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
using SoftwareConsulting.BI11.Studio;
using SoftwareConsulting.BI11.Studio.Core.DBProviders.Datasets;
using SoftwareConsulting.BI11.Studio.InternalServices;
using SoftwareConsulting.BI11.Studio.Core.DBProviders.Properties;

namespace SoftwareConsulting.BI11.Studio.Core.DBProviders
{
    /// <summary>
    /// Класс работы с БД системы
    /// </summary>
    public sealed class SystemDBProvider: DBProvider
    {     
        private DataTable _usersGeneralTable;

        public static string IdField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_ID;
            }
        }

        public static string LogOnField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_LOGON;
            }
        }

        public static string PasswordField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_PASSWORD;
            }
        }

        public static string LastNameField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_LASTNAME;
            }
        }

        public static string FirstNameField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_FIRSTNAME;
            }
        }

        public static string PatronymicField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_PATRONYMIC;
            }
        }

        public static string BirthdateField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_BIRTHDATE;
            }
        }

        public static string PostField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_POST;
            }
        }

        public static string TeamField
        {
            get
            {
                return Resources.BI_SYSTEMDB_USERS_FIELDS_TEAM;
            }
        }

        public static string FileName
        {
            get
            {
                return Resources.BI_SYSTEMDB_FILENAME;
            }
        }

        /// <summary>
        /// Конструктор класса
        /// </summary>
        public SystemDBProvider():base()
        {
            DataSet = new SystemDataSet();
            ResetInfoAboutGeneralTables();
        }

        /// <summary>
        /// Возвращает генеральную таблицу Пользователей
        /// </summary>
        public DataTable UsersGeneralTable
        {
            get
            {
                if (_usersGeneralTable == null)
                {
                    if (DataSet.Tables.Contains(Resources.BI_SYSTEMDB_USERS, Resources.BI_SYSTEMDB_NAMESPACE))
                        //уже есть(загрузили готовую систему)
                        _usersGeneralTable = DataSet.Tables[Resources.BI_SYSTEMDB_USERS, Resources.BI_SYSTEMDB_NAMESPACE];
                    else
                    {
                        _usersGeneralTable = DataSet.Tables.Add(Resources.BI_SYSTEMDB_USERS, Resources.BI_SYSTEMDB_NAMESPACE);
                        _usersGeneralTable.Columns.Add(IdField, typeof(Guid));
                        _usersGeneralTable.Columns.Add(LogOnField, typeof(string));
                        _usersGeneralTable.Columns.Add(PasswordField, typeof(string));
                        _usersGeneralTable.Columns.Add(LastNameField, typeof(string));
                        _usersGeneralTable.Columns.Add(FirstNameField, typeof(string));
                        _usersGeneralTable.Columns.Add(PatronymicField, typeof(string));
                        _usersGeneralTable.Columns.Add(BirthdateField, typeof(DateTime));
                        _usersGeneralTable.Columns.Add(PostField, typeof(string));
                        _usersGeneralTable.Columns.Add(TeamField, typeof(int));
                    }
                }
                return _usersGeneralTable;
            }
        }

        /// <summary>
        /// Перезагружает информацию о генеральных таблицах
        /// </summary>
        protected override void ResetInfoAboutGeneralTables()
        {
            _usersGeneralTable = null;
            DataTable dt = UsersGeneralTable;
        }

        /// <summary>
        /// Возвращает системную таблицу
        /// </summary>
        /// <param name="needTable"></param>
        /// <returns></returns>
        protected override DataTable GetTable(object needTable)
        {
            switch ((SystemTable)needTable)
            {
                case SystemTable.Users:
                    return UsersGeneralTable;
            }
            return null;
        }

        protected override string GetNamespaceOfZone(object tableZone)
        {
            switch ((SystemTable)tableZone)
            {
                case SystemTable.Users:
                    return Resources.BI_SYSTEMDB_NAMESPACE;
            }
            Assert.Fail(Assert.Id.Assert00173);
            return null;
        }        
    }
}